package com.ruoyi.web.controller.site;

import java.util.List;

import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBanner;
import com.ruoyi.system.service.ISysBannerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页幻灯Controller
 * 
 * @author mcrgood
 * @date 2024-08-23
 */
@Controller
@RequestMapping("/system/banner")
public class SysBannerController extends BaseController
{
    private String prefix = "system/banner";

    @Autowired
    private ISysBannerService sysBannerService;
    @Autowired
    private ServerConfig serverConfig;
    @RequiresPermissions("system:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    /**
     * 查询首页幻灯列表
     */
    @RequiresPermissions("system:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysBanner sysBanner)
    {
        startPage();
        List<SysBanner> list = sysBannerService.selectSysBannerList(sysBanner);
        for (int i=0;i<list.size();i++)
        {
            if(!StringUtils.ishttp(list.get(i).getImg()))
                list.get(i).setImg(serverConfig.getUrl()+list.get(i).getImg());
        }
        return getDataTable(list);
    }

    /**
     * 导出首页幻灯列表
     */
    @RequiresPermissions("system:banner:export")
    @Log(title = "首页幻灯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysBanner sysBanner)
    {
        List<SysBanner> list = sysBannerService.selectSysBannerList(sysBanner);
        ExcelUtil<SysBanner> util = new ExcelUtil<SysBanner>(SysBanner.class);
        return util.exportExcel(list, "首页幻灯数据");
    }

    /**
     * 新增首页幻灯
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存首页幻灯
     */
    @RequiresPermissions("system:banner:add")
    @Log(title = "首页幻灯", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysBanner sysBanner)
    {
        return toAjax(sysBannerService.insertSysBanner(sysBanner));
    }

    /**
     * 修改首页幻灯
     */
    @RequiresPermissions("system:banner:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysBanner sysBanner = sysBannerService.selectSysBannerById(id);
        mmap.put("sysBanner", sysBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存首页幻灯
     */
    @RequiresPermissions("system:banner:edit")
    @Log(title = "首页幻灯", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysBanner sysBanner)
    {
        return toAjax(sysBannerService.updateSysBanner(sysBanner));
    }

    /**
     * 删除首页幻灯
     */
    @RequiresPermissions("system:banner:remove")
    @Log(title = "首页幻灯", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysBannerService.deleteSysBannerByIds(ids));
    }
}
